package cn.jhc.sevlet;//添加学生信息

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.jhc.bean.Clazz;
import cn.jhc.bean.Student;
import cn.jhc.dao.ClassDao;
import cn.jhc.dao.StudentDao;
import cn.jhc.dao.impl.ClassDaoImpl;
import cn.jhc.dao.impl.StudentDaoImpl;

/**
 * Servlet implementation class AddinfoServlet
 */
@WebServlet("/user/teacher/addinfo.do")
public class AddinfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddinfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.sendRedirect("addinfo.jsp");
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
		String sno = request.getParameter("sno");
		String sname = request.getParameter("sname");
		String sex = request.getParameter("sex");
		String cardid = request.getParameter("cardid");
		ClassDao classDao = new ClassDaoImpl();
		String clazzname = request.getParameter("classname");
		List<Clazz> clazzs = classDao.list(clazzname);
		Clazz clazz = clazzs.get(0);
		String college = request.getParameter("college");
		Student student = new Student();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	
		try {
			System.out.println(request.getParameter("entertime")+" "+request.getParameter("gradtime")+"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
			Date entertime = sdf.parse(request.getParameter("entertime"));
			Date gradtime = sdf.parse(request.getParameter("gradtime"));
			
			student.setEntertime(entertime);
			student.setGradtime(gradtime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		student.setSpassword("123456");
		student.setSno(sno);
		student.setSname(sname);
		student.setSex(sex);
		student.setCardid(cardid);
		student.setClazz(clazz);
		StudentDao studentDao = new StudentDaoImpl();
		System.out.println(request.getContextPath());
		boolean flag;
		try{
			studentDao.save(student);
			flag = true;
		} catch (Exception e){
//			response.sendRedirect("www.baid.com");
			flag = false;
		}
//		response.sendRedirect("./tinformation.do");
		if(flag){
			response.sendRedirect("./tinformation.do");
		}
		else {
			request.setAttribute("flag", false);
			request.getRequestDispatcher("addinfo.jsp").forward(request,
					response);
		}
		
	}

}
